package com.boyi.cycling_master.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.boyi.cycling_master.R;
import com.boyi.cycling_master.application.CyclingApplication;
import com.boyi.cycling_master.bean.ResponseBean;
import com.boyi.cycling_master.common.ActivityStackManager;
import com.boyi.cycling_master.common.MoT;
import com.boyi.cycling_master.common.PostParams;
import com.boyi.cycling_master.common.SnackUtil;
import com.boyi.cycling_master.common.TextCheckedUtil;
import com.boyi.cycling_master.https.utils.UserInfoRetrofit;
import com.boyi.cycling_master.https.utils.UserRetrofit;
import com.boyi.cycling_master.ui.base.BaseActivity;
import com.boyi.cycling_master.widgets.CountDownButtonView;
import com.rengwuxian.materialedittext.MaterialEditText;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 *
 * @date 2017/8/11/011 10:48
 *
 * @author Lee_Mofeel
 *
 * @version 1.0
 *
 * @Description 修改绑定手机逻辑2
 *              提交新的手机号
 *
 */

public class PhoneChangeNewActivity extends BaseActivity {
    @BindView(R.id.btn_nav_icon)
    ImageView btnNavIcon;
    @BindView(R.id.edt_change_phone_new)
    MaterialEditText edtChangePhoneNew;
    @BindView(R.id.edt_change_phone_new_checked_code)
    MaterialEditText edtChangePhoneNewCheckedCode;
    @BindView(R.id.btn_register_get_code)
    CountDownButtonView btnRegisterGetCode;
    @BindView(R.id.tv_btn_to_next)
    TextView tvBtnToNext;

    @Override
    public int getContentViewId() {
        return R.layout.activity_change_phone_new;
    }


    @Override
    public void initLogic() {

    }

    @Override
    public void getBundleExtras(Bundle bundle) {

    }

    @OnClick({R.id.btn_nav_icon, R.id.btn_register_get_code, R.id.tv_btn_to_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_nav_icon:
                finish();
                break;
            case R.id.btn_register_get_code:
                if (!btnRegisterGetCode.isCountDown()) {
                    String phoneNum = edtChangePhoneNew.getText().toString();
                    if (TextCheckedUtil.checkPhoneNum(phoneNum)) {
                        getSmsCode(phoneNum);
                        btnRegisterGetCode.startCountDown();
                        btnRegisterGetCode.setBackgroundResource(R.drawable.login_btn_yzm_pressed);
                    }
                    else {
                        SnackUtil.SnackShort(view, "请填写您的11位手机号码");
                    }
                } else {
                    btnRegisterGetCode.stopCountDown();
                    btnRegisterGetCode.setBackgroundResource(R.drawable.btn_bangding_default);
                }
                break;
            case R.id.tv_btn_to_next:
                String phone = edtChangePhoneNew.getText().toString().toString();
                String smsCode = edtChangePhoneNewCheckedCode.getText().toString();
                if(TextCheckedUtil.checkSmsCode(smsCode) && TextCheckedUtil.checkPhoneNum(phone)){
                    submitChangePhone(phone,smsCode);
                }else{
                    SnackUtil.SnackShort(edtChangePhoneNew,"请确保您输入的信息无误");
                }
                break;
        }
    }

    /**
     * 获取验证码
     * @param phoneNum
     */
    public void getSmsCode(String phoneNum) {
        Map<String, String> map = new HashMap<>();
        map.put("userPhone", phoneNum);
        map.put("smsType", "register");
        Log.d("PostDebug onClick", "Get SMS");
        UserRetrofit.getInstance().getSmsCodeByPost(new Callback<ResponseBean>() {
            @Override
            public void onResponse(Call<ResponseBean> call, Response<ResponseBean> response) {
                if (response.body() == null)
                    return;
                SnackUtil.SnackShort(btnNavIcon, response.body().getResultMessage());
                Log.d("PostDebug onResponse", response.body().toString());
            }

            @Override
            public void onFailure(Call<ResponseBean> call, Throwable t) {
                Log.d("PostDebug onFailure", t.getMessage());
            }
        }, map);
    }

    /**
     * 提交用户修改的手机 和 验证码
     * @param phoneNum
     * @param smsCode
     */
    public void submitChangePhone(final String phoneNum, String smsCode) {
        Map<String, String> map = new HashMap<>();
        map.put("userPhone", phoneNum);
        map.put("userNumber", smsCode);
        UserInfoRetrofit.getInstance().changePhone(new Callback<ResponseBean>() {
            @Override
            public void onResponse(Call<ResponseBean> call, Response<ResponseBean> response) {
                if (response.body() == null)
                    return;
                SnackUtil.SnackShort(btnNavIcon, response.body().getResultMessage());
                if (response.body().getResultCode().equals("200")) {
                    // TODO 校验成功 进入下一步输入
                    CyclingApplication.getInstance().getUserInfo().setUserPhone(phoneNum);
                    CyclingApplication.getInstance().getUserInfo().save();
                    ActivityStackManager.getInstance().killActivity(PhoneChangeOldActivity.class);
                    finish();
                }
            }

            @Override
            public void onFailure(Call<ResponseBean> call, Throwable t) {
                SnackUtil.SnackShort(btnNavIcon, "服务器异常，请稍后重试");
            }
        }, PostParams.makeRequestBody(map));
    }
}
